package com.cq.hd.order.factory.orderSelect;

import com.cq.hd.common.enums.biz.OrderStateEnum;
import com.cq.hd.common.enums.biz.RewardTypeEnum;
import com.cq.hd.common.enums.biz.SubOrderStateEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.order.api.dto.AppOrderDetailDto;
import com.cq.hd.order.api.dto.AppOrderPageDto;
import com.cq.hd.order.api.vo.AppOrderCommissionVo;
import com.cq.hd.order.api.vo.AppOrderDetailVo;
import com.cq.hd.order.api.vo.AppOrderPageVo;
import com.cq.hd.order.po.TbOrderItemPo;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public abstract class AbstractOrderSelectHandler {

    @PostConstruct
    public void init() {
        OrderSelectFactory.register(getType(), this);
    }

    protected abstract Integer getType();

    /**
     * 订单列表
     */
    public abstract Page<AppOrderPageVo> pageAppOrder(AppOrderPageDto appOrderPageDto);

    /**
     * 订单详情
     */
    public abstract AppOrderDetailVo appOrderDetail(AppOrderDetailDto appOrderDetailDto);

    /**
     * 处理佣金权益
     */
    public BigDecimal promotionEquityHandle(Map<Long, List<TbOrderItemPo>> orderItemMap, AppOrderCommissionVo appOrderCommissionVo) {
        Long id = appOrderCommissionVo.getId();
        List<TbOrderItemPo> tbOrderItemPoList = orderItemMap.get(id);
        if (CollectionUtils.isEmpty(tbOrderItemPoList)) {
            return BigDecimal.ZERO;
        }

        Long userId = appOrderCommissionVo.getUserId();
        BigDecimal actualCommissionAmt;
        if (Objects.equals(appOrderCommissionVo.getAgentId(), userId)) {
            // 一级经纪人的佣金: 总佣金 - 二级经纪人的佣金
            actualCommissionAmt = appOrderCommissionVo.getActualCommissionAmt().subtract(appOrderCommissionVo.getSubAgentCommissionAmt());
        } else {
            // 二级经纪人的佣金
            actualCommissionAmt = appOrderCommissionVo.getSubAgentCommissionAmt();
        }

        Integer orderState = appOrderCommissionVo.getOrderState();
        // 处理佣金
        if (OrderStateEnum.WAIT_PAY.getValue().equals(orderState)
                || OrderStateEnum.WAIT_VERIFY.getValue().equals(orderState)
                || OrderStateEnum.VERIFYING.getValue().equals(orderState)) {
            //  订单待支付/待核销/核销中，显示预估佣金(全核销)
            return actualCommissionAmt;
        }
        if (OrderStateEnum.CANCEL.getValue().equals(orderState)
                || OrderStateEnum.CLOSE.getValue().equals(orderState)) {
            //  订单已取消/已关闭，不显示佣金了
//            return BigDecimal.ZERO;
            return actualCommissionAmt;
        }

        if (OrderStateEnum.FINISH.getValue().equals(orderState) || OrderStateEnum.VERIFIED.getValue().equals(orderState)) {
            //  订单已完成/已核销，显示已核销订单的对应佣金总额
            BigDecimal commissionAmt = BigDecimal.ZERO;
            BigDecimal subCommissionAmt = BigDecimal.ZERO;
            for (TbOrderItemPo tbOrderItemPo : tbOrderItemPoList) {
                if (SubOrderStateEnum.VERIFIED.getValue().equals(tbOrderItemPo.getSubOrderState()) || SubOrderStateEnum.SETTLED.getValue().equals(tbOrderItemPo.getSubOrderState())) {
                    commissionAmt = commissionAmt.add(tbOrderItemPo.getCommissionAmt());
                    subCommissionAmt = subCommissionAmt.add(tbOrderItemPo.getSubAgentCommissionAmt());
                }
            }
            if (Objects.equals(appOrderCommissionVo.getAgentId(), userId)) {
                return commissionAmt.subtract(subCommissionAmt);
            }

            return subCommissionAmt;
        }

        return BigDecimal.ZERO;
    }

    protected void setOrderDetailRewardPoints(AppOrderDetailVo appOrderDetailVo, Long userId) {
        Long agentId = appOrderDetailVo.getAgentId();
        if (appOrderDetailVo.getAgentPointsAmt() == null
                || appOrderDetailVo.getAgentPointsAmt().compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }

        // 经纪人查看
        if (this instanceof AgentOrderSelectHandler) {
            appOrderDetailVo.setRewardType(RewardTypeEnum.POINTS.getValue());
            if (userId.equals(agentId)) {
                BigDecimal subtract = appOrderDetailVo.getAgentPointsAmt().subtract(appOrderDetailVo.getSubAgentPointsAmt());
                appOrderDetailVo.setRewardPoints(subtract);
            } else {
                appOrderDetailVo.setRewardPoints(appOrderDetailVo.getSubAgentPointsAmt());
            }
        }
        // 商家查看
        if (this instanceof SellOrderSelectHandler) {
            appOrderDetailVo.setRewardType(RewardTypeEnum.POINTS.getValue());
            appOrderDetailVo.setRewardPoints(appOrderDetailVo.getAgentPointsAmt());
        }
    }

    protected void setOrderPageRewardPoints(AppOrderPageVo appOrderPageVo, Long userId) {
        Long agentId = appOrderPageVo.getAgentId();
        if (appOrderPageVo.getAgentPointsAmt() == null
                || appOrderPageVo.getAgentPointsAmt().compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        // 经纪人查看
        if (this instanceof AgentOrderSelectHandler) {
            appOrderPageVo.setRewardType(RewardTypeEnum.POINTS.getValue());
            if (userId.equals(agentId)) {
                BigDecimal subtract = appOrderPageVo.getAgentPointsAmt().subtract(appOrderPageVo.getSubAgentPointsAmt());
                appOrderPageVo.setRewardPoints(subtract);
            } else {
                appOrderPageVo.setRewardPoints(appOrderPageVo.getSubAgentPointsAmt());
            }
        }
        // 商家查看
        if (this instanceof SellOrderSelectHandler) {
            appOrderPageVo.setRewardType(RewardTypeEnum.POINTS.getValue());
            appOrderPageVo.setRewardPoints(appOrderPageVo.getAgentPointsAmt());
        }
    }

}
